package com.itchen.hospital.service.Impl;

import com.itchen.hospital.Msg.BaseExceptionInfo;
import com.itchen.hospital.Msg.DefaultInfo;
import com.itchen.hospital.common.Result;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.LoginMapper;
import com.itchen.hospital.pojo.Admin;
import com.itchen.hospital.service.LoginService;
import com.itchen.hospital.utils.JwtTokenUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final LoginMapper loginMapper;

    /**
     * 登录
     * @param admin
     * @return
     */
    @Override
    public Admin login(Admin admin) {
        //2、判断是那个角色，再去对应的表查询
        //角色为admin
        Admin adminDb = null;
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //1.先判断是否为空
        if (admin == null || admin.getAccount() == null || admin.getPassword() == null) {
            throw new CustomException(BaseExceptionInfo.AccountAndPasswordNotNull);
        }
        if (DefaultInfo.DefaultAdminName.equals(admin.getRole())) {
            adminDb = loginMapper.getAdminByAccount(admin.getAccount());
            if (adminDb == null) {
                throw new CustomException(BaseExceptionInfo.AccountNotExists);
            }
            //校验密码
            if (!encoder.matches(admin.getPassword(), adminDb.getPassword())) {
                throw new CustomException(BaseExceptionInfo.PasswordError);
            }
            // 生成jwt token给前端
            String token = JwtTokenUtils.genToken(adminDb, adminDb.getPassword());
            adminDb.setToken(token);
            adminDb.setPassword("");
            return adminDb;
        }

        //角色为doctor
        if (DefaultInfo.DefaultDoctorName.equals(admin.getRole())) {
            adminDb = loginMapper.getDoctorByAccount(admin.getAccount());
            if (adminDb == null) {
                throw new CustomException(BaseExceptionInfo.AccountNotExists);
            }
            //校验密码
            if (!encoder.matches(admin.getPassword(), adminDb.getPassword())) {
                throw new CustomException(BaseExceptionInfo.PasswordError);
            }
            // 生成jwt token给前端
            String token = JwtTokenUtils.genToken(adminDb, adminDb.getPassword());
            adminDb.setToken(token);
            adminDb.setPassword("");
            return adminDb;
        }

        //角色为user
        if (DefaultInfo.DefaultUserName.equals(admin.getRole())) {
            adminDb = loginMapper.getUserByAccount(admin.getAccount());
            if (adminDb == null) {
                throw new CustomException(BaseExceptionInfo.AccountNotExists);
            }
            //校验密码
            if (!encoder.matches(admin.getPassword(), adminDb.getPassword())) {
                throw new CustomException(BaseExceptionInfo.PasswordError);
            }
            // 生成jwt token给前端
            String token = JwtTokenUtils.genToken(adminDb, adminDb.getPassword());
            adminDb.setToken(token);
            adminDb.setPassword("");
            return adminDb;
        }

        return adminDb;
    }

    /**
     * 注册
     * @param admin
     * @return
     */
    @Override
    public Result register(Admin admin) {
        Admin adminDb = null;
        PasswordEncoder Encoder = new BCryptPasswordEncoder();
        //1.先判断是否为空
        if (admin == null || "".equals(admin.getAccount()) || "".equals(admin.getPassword())) {
            return Result.error(BaseExceptionInfo.AccountAndPasswordNotNull);
        }
        //角色为admin
        //todo 需要优化，这里全部使用了Admin类
        if (DefaultInfo.DefaultAdminName.equals(admin.getRole())) {
            adminDb = loginMapper.getAdminByAccount(admin.getAccount());
            if (adminDb != null) {
                return Result.error(BaseExceptionInfo.AccountAlreadyExists);
            }
            admin.setRole(DefaultInfo.DefaultAdminName);
            //密码进行BCrypt加密处理
            String passwordEncoder = Encoder.encode(admin.getPassword());
            admin.setPassword(passwordEncoder);
            loginMapper.insertAdmin(admin);
            return Result.success();
        }

        //角色为doctor
        if (DefaultInfo.DefaultDoctorName.equals(admin.getRole())) {
            adminDb = loginMapper.getDoctorByAccount(admin.getAccount());
            if (adminDb != null) {
                return Result.error(BaseExceptionInfo.AccountAlreadyExists);
            }
            admin.setRole(DefaultInfo.DefaultDoctorName);
            //密码进行BCrypt加密处理
            String passwordEncoder = Encoder.encode(admin.getPassword());
            admin.setPassword(passwordEncoder);
            loginMapper.insertDoctor(admin);
            return Result.success();
        }

        //角色为user
        if (DefaultInfo.DefaultUserName.equals(admin.getRole())) {
            adminDb = loginMapper.getUserByAccount(admin.getAccount());
            if (adminDb != null) {
                return Result.error(BaseExceptionInfo.AccountAlreadyExists);
            }
            admin.setRole(DefaultInfo.DefaultUserName);
            //密码进行BCrypt加密处理
            String passwordEncoder = Encoder.encode(admin.getPassword());
            admin.setPassword(passwordEncoder);
            loginMapper.insertUser(admin);
            return Result.success();
        }


        return null;
    }
}
